import numpy as np
from tkinter import filedialog


class Maze(object):

    def __init__(self, cv, windows_h, bd, win, map_size=32):
        self.map_size = map_size
        self.cv = cv
        self.bd = bd
        self.win = win
        self.up = 0
        self.down = 1
        self.left = 2
        self.right = 3
        self.maze_map = np.ones((map_size, map_size, 4), dtype='int8')  # h/y, w/x, wall/up,down,left,right
        self.cell_len = int((windows_h-2*self.bd)/map_size)
        self.line_hand = []
        self.win_point = None

    def draw_win_point(self):
        # clear win point
        if self.win_point is not None:
            self.cv.delete(self.win_point)
        # draw win point
        w, h = self.win
        x, y = (w+0.5)*self.cell_len+self.bd, (h+0.5)*self.cell_len+self.bd
        rate = self.cell_len/3
        x0, y0 = x - rate, y - rate
        x1, y1 = x + rate, y + rate
        self.win_point = self.cv.create_oval(x0, y0, x1, y1, fill='red')

    def draw_maze(self):
        # clear maze
        for tag in self.line_hand:
            self.cv.delete(tag)
        # draw maze
        for h in range(self.map_size):
            for w in range(self.map_size):
                for index in range(4):
                    # up and down for wall
                    if index in [self.up, self.down]:
                        x0, y0 = w*self.cell_len+self.bd, (h+index)*self.cell_len+self.bd
                        x1, y1 = (w+1)*self.cell_len+self.bd, (h+index)*self.cell_len+self.bd
                    # left and right for wall
                    else:
                        x0, y0 = (w+index-2)*self.cell_len+self.bd, h*self.cell_len+self.bd
                        x1, y1 = (w+index-2)*self.cell_len+self.bd, (h+1)*self.cell_len+self.bd
                    # no line draw white line
                    if self.maze_map[h, w, index] == 1:
                        color = 'black'
                    else:
                        color = 'Gainsboro'
                    # draw line
                    self.line_hand.append(self.cv.create_line(x0, y0, x1, y1, width=2, fill=color))
                    self.cv.tag_bind(self.line_hand[-1], '<Button-1>', lambda event, c=[h, w, index]: self.change_cell(c))

    def change_cell(self, c):
        # click to hide or show line
        h, w, index = c
        # edge wall
        if h == 0 and index == self.up:
            return
        if w == 0 and index == self.left:
            return
        # up wall
        if index == self.up:
            # deal clicked line
            if self.maze_map[h, w, self.up] == 1:
                self.maze_map[h, w, self.up] = 0
            else:
                self.maze_map[h, w, self.up] = 1
            # deal adjoin line
            if h-1 >= 0:
                if self.maze_map[h, w, self.up] == 0:
                    self.maze_map[h - 1, w, self.down] = 0
                else:
                    self.maze_map[h - 1, w, self.down] = 1
        # left wall
        if index == self.left:
            # deal clicked line
            if self.maze_map[h, w, self.left] == 1:
                self.maze_map[h, w, self.left] = 0
            else:
                self.maze_map[h, w, self.left] = 1
            # deal adjoin line
            if w-1 >= 0:
                if self.maze_map[h, w, self.left] == 0:
                    self.maze_map[h, w - 1, self.right] = 0
                else:
                    self.maze_map[h, w - 1, self.right] = 1
        self.draw_maze()

    def save_maze(self):
        fileSave = filedialog.asksaveasfilename(defaultextension='.txt', filetypes=[("txt files", ".txt")])
        np.savetxt(fileSave, self.maze_map.reshape((1, self.map_size*self.map_size*4)), fmt='%d')

    def load_maze(self):
        filePath = filedialog.askopenfilename()
        self.maze_map = np.loadtxt(filePath).reshape((self.map_size, self.map_size, 4))
        self.draw_maze()

    def get_maze_map(self):
        return self.maze_map.copy()


